package com.lvyq.javaprint.view.tray;

import com.lvyq.javaprint.constant.ImgConstant;
import com.lvyq.javaprint.constant.TrayConstant;
import com.lvyq.javaprint.util.CommonUtils;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-04-28 10:29
 */
public class TrayMainView {
    private static TrayIcon trayIcon;

    private static Stage trayStage;

    private static Stage trayAuxStage;

    /**
     * @description 创建托盘
     * @author lvyq
     * @param[1] primaryStage
     * @throws
     * @time 2024-04-24 17:00
     */
    public static void createTray(Stage primaryStage) {
            try {
                java.awt.Image image = CommonUtils.getImage(ImgConstant.ICON_URL);
                SystemTray tray = SystemTray.getSystemTray();
                trayIcon = new TrayIcon(image, TrayConstant.APP_TITLE);
                // 关键点，设置托盘图标的自适应属性
                trayIcon.setImageAutoSize(true);
                tray.add(trayIcon);
                //创建托盘窗体
                createTrayStage();
                //创建监听事件
                createTrayListener(tray,primaryStage);
            } catch (Exception e) {
                e.printStackTrace();
                // 处理异常
            }

    }


    /**
     * @description 创建托盘窗体
     * @author lvyq
     * @throws
     * @time 2024-04-29 10:21
     */
    public static void createTrayStage(){
        trayAuxStage=new Stage();
        trayAuxStage.initStyle(StageStyle.UTILITY);
        trayAuxStage.setScene(new Scene(new StackPane(),1,1));
        trayAuxStage.setX(Double.MAX_VALUE);
        trayAuxStage.show();

        trayStage = new Stage();
        trayStage.initStyle(StageStyle.TRANSPARENT);
        trayStage.initOwner(trayAuxStage);
        Scene scene = new Scene(TrayButtonView.createOperateMenu(trayStage));
        scene.getStylesheets().add("css/style.css");
        trayStage.setScene(scene);
        trayStage.setAlwaysOnTop(true);
        trayStage.setHeight(200);
        trayStage.setWidth(TrayConstant.TRAY_STAGE_WIDTH);
        //默认隐藏
        trayStage.hide();

        // 监听Stage的焦点状态,失去焦点隐藏
        trayStage.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue){
                    if (trayStage.isShowing()){
                        trayStage.hide();
                    }
                }
            }
        });

    }


    /**
     * @description  创建监听事件
     * @author lvyq
     * @throws
     * @time 2024-05-14 14:43
     */
    private static void createTrayListener(SystemTray tray,Stage primaryStage) {
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //鼠标左键双击
                doubleClick(e,tray,primaryStage);
                //显示托盘
                showTrayView(e);
            }
        });
    }

    /**
     * @description 双击事件处理
     * @author lvyq
     * @param[1] tray
     * @param[2] primaryStage
     * @throws
     * @time 2024-05-14 14:50
     */
    private static void doubleClick(MouseEvent e ,SystemTray tray, Stage primaryStage) {
        if (e.getClickCount()==2 && e.getButton()==MouseEvent.BUTTON1){
            //从托盘区域移去托盘对
            tray.remove(trayIcon);
            //显示窗口
            Platform.runLater(primaryStage::show);
        }
    }

    /**
     * @description 显示托盘
     * @author lvyq
     * @param[1] e
     * @throws 
     * @time 2024-05-14 14:48
     */
    private static void showTrayView(MouseEvent e) {
        if (e.getButton()==MouseEvent.BUTTON3){
            Platform.runLater(()->{
                //托盘位置设置
                trayStage.setX(e.getXOnScreen()/ Screen.getPrimary().getOutputScaleX());
                trayStage.setY(e.getYOnScreen()/Screen.getPrimary().getOutputScaleY()-trayStage.getHeight());
                trayStage.show();
            });
        }
    }
}
